/**
 * 张志浩(作者)
 * 2018-05-21(开始日期)
 * 2018--(最终更改日期)
 * xxx(最后更改者)
 * */
//var apicom="http://192.168.30.226:10001/"
var ua = navigator.userAgent.toLowerCase(),
  mark_s;
var isDownload = true; //判断是否直接下载
if (/android/.test(ua)) {
  //安卓手机
  mark_s = 0;
} else if (/iphone/.test(ua)) {
  //苹果手机
  mark_s = 1;

}
var share_phone = globalFun.GetQueryString("phone")
var token = globalFun.GetQueryString("token")
$(function() {
  /* 手机号输入校验 */
  $("#phone").on("input", function() {
    this.value = this.value.replace(/\D/, "");
  })
  /* 手机号输入校验 */
  $("#code").on("input", function() {
    this.value = this.value.replace(/\D/, "");
  })
  //获取手机验证码
  $('#getCode').click(function() {
    isDownload = false;
    var phoneNum = $('#phone').val();
    var $this = $(this)
    if ($(this).hasClass("disabled")) {
      return false;
    }
    if (phoneNum == "") {
      layer.open({
        content: "被邀请人手机号不能为空",
        skin: 'msg',
        time: 1 //2秒后自动关闭
      });
      return false;
    }
    if (phoneNum.length < 11) {
      layer.open({
        content: "请检查手机号是否正确",
        skin: 'msg',
        time: 1 //2秒后自动关闭
      });
      return false;
    }
    $(this).addClass("disabled")
    var dataObj = {
      "phone": phoneNum,
      "type": 3
    }
    $.ajax({
      url: apicom + "mobile/view/member/sendSms",
      type: 'POST',
      dataType: 'json',
      data: dataObj,
      success: function(result) {
        var res;
        if (typeof(result) == "string") {
          res = JSON.parse(result)
        } else {
          res = result
        }
        if (res.code == 1) {
          var count = 60;
          var timer = setInterval(function() {
            count--;
            $this.html("重新发送" + count)
            if (count <= 0) {
              clearInterval(timer);
              $this.html("重新发送");
              $this.removeClass("disabled")
            }
          }, 1000)
        } else {
          $this.removeClass("disabled");
        }
        //提示
        layer.open({
          content: res.msg,
          skin: 'msg',
          time: 1 //2秒后自动关闭
        });
      },
      error: function(res) {
        $this.removeClass("disabled")
        //提示
        layer.open({
          content: "服务器连接失败",
          skin: 'msg',
          time: 2 //2秒后自动关闭
        });
      }
    })
  })

  //校验手机验证码
  $('.android').click(function() {
    download(1)
  })
  $('.iphone').click(function() {
    download(0)
  })
})

function download(type) {

  if (mark_s === type) {
    //提示
    layer.open({
      content: "您选择的设备与您当前设备不符",
      skin: 'msg',
      time: 2 //2秒后自动关闭
    });
    return false
  }
  if (isDownload) {
    if (mark_s === 0) {
      location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=com.yst.onecity&channel=0002160650432d595942&fromcase=60001"
    } else {
      location.href = "https://itunes.apple.com/us/app/%E6%99%AE%E6%B5%8E%E4%B8%80%E5%9F%8E/id1352933890?l=zh&ls=1&mt=8"
    }
    return false;
  }
  var dataObj = {
    "phone": share_phone,
    "token": token,
    "rePhone": $('#phone').val(),
    "smsCode": $('#code').val(),
    "mark": mark_s
  }
  if (!dataObj.rePhone) {
    //提示
    layer.open({
      content: "请输入手机号",
      skin: 'msg',
      time: 1 //2秒后自动关闭
    });
    return false;
  }
  if (!dataObj.smsCode) {
    //提示
    layer.open({
      content: "请输入验证码",
      skin: 'msg',
      time: 1 //2秒后自动关闭
    });
    return false;
  }
  $.ajax({
    url: apicom + "mobile/view/member/invitation-register",
    type: 'POST',
    dataType: 'json',
    data: dataObj,
    success: function(result) {
      var res;
      if (typeof(result) == "string") {
        res = JSON.parse(result)
      } else {
        res = result
      }
      if (res.code == 1) {
        setTimeout(function() {
          if (mark_s === 0) {
            location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=com.yst.onecity&channel=0002160650432d595942&fromcase=60001"
          } else {
            location.href = "https://itunes.apple.com/us/app/%E6%99%AE%E6%B5%8E%E4%B8%80%E5%9F%8E/id1352933890?l=zh&ls=1&mt=8"
          }
        }, 1000)
      }
      //提示
      layer.open({
        content: res.msg,
        skin: 'msg',
        time: 1 //2秒后自动关闭
      });
    },
    error: function(res) {
      //提示
      layer.open({
        content: "服务器连接失败",
        skin: 'msg',
        time: 2 //2秒后自动关闭
      });
    }
  })
}